Plotting Every Crash
counties <- map_data('county')
chicago_box <- c(left = -87.936287,
bottom = 41.679835,
right = -87.447052,
top = 42.000835)
chicago_map <- get_stamenmap(chicago_box, zoom = 12)
chicago_map %>%
ggmap() +
geom_point(data = crash_with_injury, aes(x=LONGITUDE,y=LATITUDE),size=0.2)

chicago_map %>%
ggmap() +
geom_point(data = crash_with_injury, aes(x=LONGITUDE,y=LATITUDE),size=0.2) +
geom_density2d(data = crash_with_injury, aes(x=LONGITUDE,y=LATITUDE), size=0.5)

downtown_box <- c(left = -87.656248,
bottom = 41.862511,
right = -87.596938,
top = 41.901527)
downtown_map <- get_stamenmap(downtown_box, zoom = 14)
# Make pretty plot
downtown_crash_map <- downtown_map %>%
ggmap() +
ggtitle("Car Crashes in Downtown Chicago",
subtitle = "An interactive map") +
scale_size(range = c(2,10)) +
geom_point(data = crash_with_injury,
aes(x=LONGITUDE,
y=LATITUDE,
fill=`Road Conditions`,
text = paste(
"Street: ", STREET_NAME, "\n",
"Date: ", date, "\n",
"Damage: ", DAMAGE, "\n",
"Road Condition: ", `Road Conditions`, "\n",
"Injuries: ", INJURIES_TOTAL,
sep = ""),
size=INJURIES_TOTAL,alpha=0.94),
colour="black",
pch=21) +
theme(axis.title = element_blank(),
legend.title = element_blank(),
legend.position = c(1,1),
legend.background = element_rect(fill = "#CFCFCF"),
plot.title = element_text(family = "Arial Black",
hjust = 0.5,
size = 20,
colour = "#5C5C5C"))
# Plot graph and make interactive with plotly
ggplotly(downtown_crash_map, tooltip = "text") %>%
layout(hoverlabel=list(bgcolor="lightblue", alpha=0.8),
title=list(text=paste0('Car Crashes in Downtown Chicago',
'<br>',
'<sup>',
'An interactive map of crashes in 2016',
'</sup>')))